<%@ page import="DB.*" %>
<%@ page import="POJO.*" %>

<%
            User u = (User) session.getAttribute("user");
            u.GetInfo();
            TripManager tm = new TripManager(DBInfo.getConnection());
            if (!tm.isConnected()) {
                out.print("<h2 class=\"nice\">Error: No connection with the database.</h2>");
            } else if (u.username == null) {
                out.print("<h2 class=\"nice\">Error: User not logged in.</h2>");
            } else if (request.getParameter("tripname") != null && request.getParameter("start") != null && request.getParameter("end") != null) {
                if (tm.getTripIdByName(request.getParameter("tripname")) != -1) {
                    out.print("<h2 class=\"nice\">Error: There already exists a trip with this name</h2>");
                } else if (request.getParameter("start").equals("") || request.getParameter("end").equals("")) {
                    out.print("<h2 class=\"nice\">Error: Both dates must be set</h2>");
                } else if (request.getParameter("start").compareTo(request.getParameter("end")) > 0) {
                    out.print("<h2 class=\"nice\">Error: End date is before start date</h2>");
                } else {
                    int id = tm.addTrip(request.getParameter("tripname"), u.username, request.getParameter("start"), request.getParameter("end"));%>
<div class="nice">
    <h1 class="nice">Trip <%out.print(request.getParameter("tripname"));%> succesfully added.</h1>
    &nbsp;&nbsp;&nbsp;&nbsp;To add locations to this trip, please click <a class="nice" href="index.jsp?page=trip&amp;tripid=<%out.print(id);%>">here</a>.
</div>
<% try {
            RSSCreator.createRSS();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
} else {%>
<script type="text/javascript" src="jquery-1.2.6.min.js"></script>
<script type="text/javascript" src="date.js"></script>
<script type="text/javascript" src="datePicker.js"></script>
<script type="text/javascript" charset="utf-8">
    $(function()
    {
        Date.format = 'yyyy/mm/dd';
        $('.date-pick').datePicker({clickInput:true});
        $('.date-pick').dpSetStartDate('1990/01/01');
        
        $('#start').bind(
        'dpClosed',
        function(e, selectedDates)
        {
            var d = selectedDates[0];
            if (d) {
                d = new Date(d);
                $('#end').dpSetStartDate(d.addDays(1).asString());
            }
        }
    );
        $('#ende').bind(
        'dpClosed',
        function(e, selectedDates)
        {
            var d = selectedDates[0];
            if (d) {
                d = new Date(d);
                $('#start').dpSetEndDate(d.addDays(-1).asString());
            }
        }
    );
    });
    
    function checkform(form) {
        if (form.tripname.value.length < 3) {
            alert("Trip name should be at least 3 characters long!");
            form.tripname.focus();
            return false;
        }
        else if (form.start.value.match(/^\d{4}\/\d{2}\/\d{2}$/) != form.start.value || form.end.value.match(/^\d{4}\/\d{2}\/\d{2}$/) != form.end.value) {
            alert("Both dates should be filled in and valid!\nValid format: yyyy/mm/dd");
            form.start.focus();
            return false;
        }
        else if (form.start.value > form.end.value) {
            alert("Start date should be before (or equal to) end date!");
            form.start.focus();
            return false;
        }
        else
            return true;
    }
</script>

<div class="nice">
    <h2>Add a new trip</h2>
    <form action="index.jsp?page=addTrip" method="post" onsubmit="return checkform(this)">
        <table class="nice" width="100%" style="text-align:left;">
            <tr><td class="b">Name:</td><td>&nbsp;&nbsp;&nbsp;<input size="35" type="text" name="tripname" id="tripname" /></td></tr>
            <tr><td class="b" colspan="2"><label for="start">Start Date:</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input size="50" type="text" class="date-pick" id="start" name="start" /></td></tr>
            <tr><td class="b" colspan="2"><label for="end">End Date:</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input size="50" type="text" class="date-pick" id="end" name="end" /></td></tr>
            <tr><td colspan="2" align="center"><br /><input type="submit" value="Create New Trip" /></td></tr>
        </table>
    </form>
</div>

<% }%>
